package com.jlnk.stockdemo.viewmodel;

import android.util.Log;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jlnk.stockdemo.bean.StockDetailRequest;
import com.jlnk.stockdemo.bean.StockDetailResponse;
import com.jlnk.stockdemo.bean.StockListRequest;
import com.jlnk.stockdemo.bean.StockListResponse;
import com.jlnk.stockdemo.callback.ResponseCallback;
import com.jlnk.stockdemo.utils.NetworkUtils;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.List;

public class StockViewModel extends ViewModel {
    private static final String TAG = "StockViewModel";
    private MutableLiveData<List<StockListResponse.Data.Stock>> stockListLiveData;
    private MutableLiveData<List<StockDetailResponse.Data.StockDetail>> stockDetailLiveData;

    public StockViewModel() {
        stockListLiveData = new MutableLiveData<>();
        stockDetailLiveData = new MutableLiveData<>();
    }

    public LiveData<List<StockListResponse.Data.Stock>> getStockListLiveData() {
        return stockListLiveData;
    }

    public MutableLiveData<List<StockDetailResponse.Data.StockDetail>> getStockDetailLiveData() {
        return stockDetailLiveData;
    }

    // 获取股票列表
    public void fetchStockList() {
        StockListRequest stockListRequest = new StockListRequest();
        NetworkUtils.sendPostRequest(stockListRequest, new ResponseCallback() {
            @Override
            public void onResponse(String response) {
                // 在这里处理响应数据
                Gson gson = new Gson();
                Type type = new TypeToken<StockListResponse>(){}.getType();
                StockListResponse stockListResponse = gson.fromJson(response, type);
                if (stockListResponse != null) {
                    // 在这里处理解析后的数据
                    List<StockListResponse.Data.Stock> stockList = stockListResponse.getData().getStockList();
                    stockListLiveData.postValue(stockList);
                }
            }

            @Override
            public void onPermissionError(String msg) {
                Log.d(TAG, "onPermissionError: " + msg);
            }

            @Override
            public void onInternalError(String msg) {
                Log.d(TAG, "onInternalError: " + msg);
            }

            @Override
            public void onFailure(IOException e) {
                Log.d(TAG, "onFailure: " + e.getMessage());
            }
        });
    }

    // 获取某个股票详细行情
    public void fetchStockDetail(String ts_code) {
        StockDetailRequest stockDetailRequest = new StockDetailRequest();
        StockDetailRequest.Params params = new StockDetailRequest.Params();
        params.setTs_code(ts_code);
        params.setLimit("20");
        stockDetailRequest.setParams(params);
        NetworkUtils.sendPostRequest(stockDetailRequest, new ResponseCallback() {
            @Override
            public void onResponse(String response) {
                // 在这里处理响应数据
                Gson gson = new Gson();
                Type type = new TypeToken<StockDetailResponse>(){}.getType();
                StockDetailResponse stockDetailResponse = gson.fromJson(response, type);
                if (stockDetailResponse != null) {
                    // 在这里处理解析后的数据
                    List<StockDetailResponse.Data.StockDetail> stockDetailList = stockDetailResponse.getData().getStockDetailList();
                    stockDetailLiveData.postValue(stockDetailList);
                }
            }

            @Override
            public void onPermissionError(String msg) {
                Log.d(TAG, "onPermissionError: " + msg);
            }

            @Override
            public void onInternalError(String msg) {
                Log.d(TAG, "onInternalError: " + msg);
            }

            @Override
            public void onFailure(IOException e) {
                Log.d(TAG, "onFailure: " + e.getMessage());
            }
        });
    }
}
